<template>
	<!-- 创建节点 -->
	<view class="viewlifehistory" :style="'background-image: url('+photo_cdn+'zpupload/20230908/746055724299c6b0c5.png);'" >
		<view class="already_sculpture" v-if="clan_info.photo">
			<view class="img_box">
				<image class="alaready_img" :src="photo_cdn+clan_info.photo" mode="aspectFill"></image>
			</view>
			
			<image @tap.stop="deleteimg" class="alaready_delete" :src="photo_cdn+'zpupload/static/img/delicons.png'"></image>
		</view>
		<view v-else class="upload_sculpture" @tap="uploadimg">
			<view class="shulines"></view>
			<view class="henglines"></view>
		</view>

		<view class="zupulable_from">
			<view class="lable_cell_items">
				<view class="lable_names">姓（必填）</view>
				<view class="lable_input_view">
					<input class="lable_input_tag" v-model="clan_info.surname" type="text" value="" placeholder="请输入姓氏" ></input>
				</view>
			</view>
			
			<view class="lable_cell_items">
				<view class="lable_names">名（必填）</view>
				<view class="lable_input_view">
					<input class="lable_input_tag" v-model="clan_info.firstname" type="text" value="" placeholder="请输入名字" ></input>
				</view>
			</view>
			
			<!-- 下拉框 -->
			<view class="lable_cell_items" @tap="getuserxb">
				<view class="lable_names">性别（必填）</view>
				<view class="lable_input_view">
					<view class="lable_input_tag" v-if="clan_info.sex===1">男</view>
					<view class="lable_input_tag" v-else-if="clan_info.sex===2">女</view>
					<view class="lable_input_tag hdcolor" v-else>请选择性别</view>
				</view>
			</view>
			
			<view class="lable_cell_items">
				<view class="lable_names">世代（必填）</view>
				<view class="lable_input_view">
					<input class="lable_input_tag" v-model="clan_info.level" :disabled="leveldisabled" type="number" value="" placeholder="请输入您是第几代人 如: 1"></input>
				</view>
			</view>
			
			<picker @change="bindPickerChange" :value="suite" :range="inferiority">
				<view class="lable_cell_items">
					<view class="lable_names">字辈</view>
					<view class="lable_input_view">
						<input class="lable_input_tag" :disabled="true" v-model="clan_info.ext5" type="text" value="" placeholder="请选择字辈"></input>
					</view>
				</view>
			</picker>
			
			<view v-if="source!=1" class="lable_cell_items">
				<view class="lable_names">家中排行</view>
				<view class="lable_input_view">
					<input class="lable_input_tag" v-model="clan_info.ranking" type="number" value="" placeholder="请输入您在家中排行老几 如: 1"></input>
				</view>
			</view>
			
			<view v-if="source!=1" class="lable_cell_items">
				<view class="lable_names">谱名</view>
				<view class="lable_input_view">
					<input class="lable_input_tag" v-model="clan_info.ext8" type="text" value="" placeholder="请输入您的谱名"></input>
				</view>
			</view>
			
			<!-- 下拉框 -->
			<view class="lable_cell_items">
				<view class="lable_names">出生日期</view>
				<view class="lable_input_view">
					<input :disabled="false" v-model="clan_info.ext1" class="lable_input_tag" type="text" value="" placeholder="如: X年X月X日 或 乾隆X年" ></input>
				</view>
			</view>
			
			
			<!-- 下拉框 -->
			<!-- <lpicker v-model="trueAddr" @clearelech="clearelech" @select="lpicker_select"> -->
				<view class="lable_cell_items">
					<view class="lable_names">出生地</view>
					<view class="lable_input_view">
						<input class="lable_input_tag" type="text" v-model="trueAddr" value="" :disabled="false" placeholder="如: X省X市X县" ></input>
					</view>
				</view>
			<!-- </lpicker> -->
			
			<!-- 下拉框 -->
			<view class="lable_cell_items" @tap="select_zai">
				<view class="lable_names">是否在世（必填）</view>
				<view class="lable_input_view">
					<view class="lable_input_tag" v-if="clan_info.ext0===0">是</view>
					<view class="lable_input_tag" v-else-if="clan_info.ext0===1">否</view>
					<view class="lable_input_tag hdcolor" v-else>请选择在世情况</view>
				</view>
			</view>
			
			<!-- 下拉框 @tap="showpicker2"-->
			<view class="lable_cell_items" v-if="clan_info.ext0===1">
				<view class="lable_names">辞世日期</view>
				<view class="lable_input_view">
					<input :disabled="false" v-model="clan_info.ext2" class="lable_input_tag" type="text" value="" placeholder="请输入辞世日期" ></input>
				</view>
			</view>
			
			<!-- ？ -->
			<view class="lable_cell_items" v-if="clan_info.ext0===1">
				<view class="lable_names">安息地</view>
				<view class="lable_input_view">
					<input class="lable_input_tag" type="text" v-model="clan_info.sleep_place" value="" placeholder="请输入安息地" ></input>
				</view>
			</view>
			
			
			
			<view class="lable_cell_items">
				<view class="lable_names">个人介绍</view>
				<view class="lable_input_view">
					<!-- <input class="lable_input_tag" type="text" value="" placeholder="请输入个人介绍" ></input> -->
					<textarea class="lable_textarea_tag" maxlength="-1" v-model="clan_info.ext3"  placeholder="如: 为人正直善良, 孝顺父母, 乐于助人, 工作认真, 刻苦钻研"></textarea>
				</view>
			</view>
			
			
			<view class="lable_cell_items">
				<view class="lable_names">生平事迹</view>
				<view class="lable_input_view">
					<textarea class="lable_textarea_tag"  maxlength="-1" v-model="clan_info.ext4"  placeholder="如: 曾获得市级xx比赛一等奖荣誉, 热爱家乡, 为家乡建设无私奉献, 备受邻里亲友尊敬"></textarea>
				</view>
			</view>
			
			<view class="lable_cell_items">
				<view class="lable_names">教育经历</view>
				<view class="lable_input_view">
					<view class="exper_box">
						<view class="edu_info" v-for="(item,index) in clan_info.ext6" :key="index" @tap="toschool(index)">
							<view>
								<view class="edu_info_title">{{item.school}}</view>
								<view class="flex">
									<view class="edu_info_txt">{{item.education}}</view>
									<view v-if="item.major" class="edu_info_txt">{{item.major}}</view>
									<view class="edu_info_txt">{{item.time}} 入学</view>
								</view>
							</view>
							<image class="eduimg" src="/sta1tic/user/jiantoua.png" mode="heightFix"></image>
						</view>
						<navigator url="/pages/mine/myschool/myschool" hover-class="none" class="exper_btn">添加学校</navigator>
					</view>
				</view>
			</view>
			
		<!-- 	<view class="lable_cell_items">
				<view class="lable_names">教育经历</view>
				<view class="lable_input_view">
					<view class="exper_box" style="margin-bottom: 30rpx;">
						<u-time-line style="margin-top: 30rpx;">
							<u-time-line-item   v-for="(item,index) in clan_info.ext6" :key="index" @tap="toschool(index)">
								<template v-slot:content> 
									<view>
										<view class="u-order-title">{{item.major}}</view>
										<view class="u-order-desc">{{item.school}}</view>
										<view class="u-order-time">{{item.time}} 入学</view>
									</view>
								</template>
							</u-time-line-item>
						</u-time-line>
						
						<navigator url="/pages/mine/myschool/myschool" hover-class="none" class="exper_btn">添加学校</navigator>
					</view>
				</view>
			</view> -->
			
			
			
			<view class="lable_cell_items">
				<view class="lable_names">工作经历</view>
				<view class="lable_input_view">
					<view class="exper_box">
						<view class="edu_info" v-for="(item,index) in clan_info.ext7" :key="index" @tap="towork(index)">
							<view>
								<view class="edu_info_title">{{item.company}}</view>
								<view class="flex">
									<view class="edu_info_txt">{{item.job}}</view>									
									<view class="edu_info_txt">{{item.time}}</view>
								</view>
							</view>
							<image class="eduimg" :src="photo_cdn+'zpupload/static/user/jiantoua.png'" mode="heightFix"></image>
						</view>
						<navigator url="/pages/mine/addcompany/addcompany" hover-class="none" class="exper_btn">添加单位</navigator>
					</view>
				</view>
			</view>
			
			<view style="font-size: 30rpx;font-weight: 800;color: #295B6E;">*为避免不必要的麻烦，请仔细再三确认族员的姓名，性别以及在世情况后，再创建</view>
		</view>
		
		<w-picker
		    mode="date" 
		    startYear="100"
		    endYear="2030"
		    :defaultVal="defaultVal1" 
			:selectList="selectList1"
		    :current="true" 
		    @confirm="onConfirm1" 
		    ref="picker1" 
		    themeColor="#5771FF"
		></w-picker>
		<w-picker
		    mode="date" 
		    startYear="100"
		    endYear="2030"
		    :defaultVal="defaultVal1" 
			:selectList="selectList1"
		    :current="true" 
		    @confirm="onConfirm2" 
		    ref="picker2" 
		    themeColor="#5771FF"
		></w-picker>
		<!-- 选时间组件 -->
		
		<view class="sub_footbtn">
			<view class="sub_button" @tap="toFound">确定添加</view>
		</view>
	</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";//下拉选择组件
	import lpicker from "@/components/lpicker/lpicker.vue";
	import config from "@/utils/config.js";
	import {getUserinfo} from "@/utils/api/api.js";
	import {addCreateFamily,addChildFamily,dimFamilyPeople,addMateFamily,addParentFamily,genealogyList} from "@/utils/api/zupu.js" 
	let {uploadFile,photo_cdn} = config.baseUrl;
	import {mapActions} from "vuex";
	
	import debug from "@/utils/debug.js";
	let {apple_isdebug} = debug
	console.info("apple_isdebug",apple_isdebug);
	export default {
		components:{
			lpicker,
			wPicker,
		},
		data() {
			return {
				pu_type:"",//谱的类型 1家谱 2族谱  3宗谱
				apple_isdebug,
				source:"",//操作来源  1添加配偶   2添加父辈  3 添加子辈  4 初始化节点
				operate_name:"",//操作该人的名字
				clan_info:{
					surname:'',//姓
					firstname:'',//名
					sex:"",//1男  2女	
					ext0:"",//是否在世 0 是   1 否
					ext1:"",//出生日期
					ext2:'',//辞世时间
					ext3:'',//个人简介
					ext4:'',//生平事迹
					ext5:'',//字辈
					ranking:"",//排行
					ext6:[],//教育经历
					ext7:[],//工作经历
					ext8:'',//谱名
					level:'',//世代
					photo:"",//照片
					province_id:"",//省id
					city_id:"",//市id
					area_id:"",//区县id
					town_id:"",//街道id
					village_id:"",//村管委会
					sleep_place:'',//安息地
				},
				
				defaultVal1:[],
				selectList1:[],
				query_result:{},//查询结果
				
				photo_cdn,
				family_id:'',//族谱id
				parent_id:'',//父级id
				child_id:'',//子级id
				family_tree_id:'',//配偶id（操作人的id）
				people_id:'',//本人id
					
				trueAddr:"",//祖籍文字
				people_number:'',//付款时需要带过去的编号
				
				userInfo:{},
				is_model:false,
				inferiority:[],//字辈
				suite:0,
				leveldisabled:false,//是否禁用世代 数字
				
			}
		},
		async onLoad(option){
			this.pu_type = option.pu_type?option.pu_type:"1"
			this.source = option.source?parseInt(option.source):"";
			this.operate_name = option.operate_name?option.operate_name:'';
			this.family_id = option.family_id;//整个族谱的id
			this.parent_id = option.parent_id?option.parent_id:'';//添加子辈时传的父级id
			this.family_tree_id = option.family_tree_id?option.family_tree_id:'';//添加配偶时传的配偶id
			this.child_id = option.child_id?option.child_id:'';
			this.people_id = option.people_id?option.people_id:'';
			this.clan_info.surname = option.surname?option.surname:'';
			this.clan_info.level = option.level?option.level:"";//世代
			if(this.clan_info.level){
				this.leveldisabled = true;
			}
			this.getUserinfofun();
			this.genealogyListfun();
			let navtitle = "创建节点";
			switch(this.source)
			{
			    case 1:
			        navtitle = `给『${this.operate_name}』添加配偶`;
			        break;
			    case 2:
			        navtitle = `给『${this.operate_name}』添加父辈`;
			        break;
				case 3:
				    navtitle = `给『${this.operate_name}』添加子辈`;
				    break;
				case 4:
				    navtitle = `初始化节点`;
				    break;
			}
			uni.setNavigationBarTitle({
				title:navtitle,
			})
		},
		onShow(){
			this.clan_info.ext6 = uni.getStorageSync("educational");//教育经历
			this.clan_info.ext7 = uni.getStorageSync("work_experience");//教育经历
		},
		onUnload(){
			uni.removeStorageSync('educational');
			uni.removeStorageSync('work_experience');
		},
		methods:{
			...mapActions(['ModifyItemData','ModifyItemDataTop','familyTreeListfun']),
			showpicker1(){
				this.$refs.picker1.show();
			},
			showpicker2(){
				this.$refs.picker2.show();
			},
			getUserinfofun() {
				getUserinfo({
			
				}).then((res) => {
					this.userInfo = res.data.userinfo;
				})
			},
			//日期选择点击后 重置表格
			onConfirm1(e){
				//console.info('e 选时间',e);
				this.clan_info.ext1 = e.result;
			},
			onConfirm2(e){
				//console.info('e 选时间',e);
				this.clan_info.ext2 = e.result;
			},
			//选择性别
			getuserxb(){
				uni.showActionSheet({
					itemList: ['男','女'],
					success:  (res)=> {
						if(res.tapIndex==0){
							this.clan_info.sex = 1;
						}else if(res.tapIndex==1){
							this.clan_info.sex = 2;
						}
					},
					fail:  (res)=> {
						console.log(res.errMsg);
					}
				});
			},
			//选择是否辞世
			select_zai(){
				uni.showActionSheet({
					itemList: ['是','否'],
					success:  (res)=> {
						if(res.tapIndex==0){
							this.clan_info.ext0 = 0;
						}else if(res.tapIndex==1){
							this.clan_info.ext0 = 1;
						}
					},
					fail:  (res)=> {
						console.log(res.errMsg);
					}
				});
			},
			//清楚选中的五级地址数据
			clearelech(e){
				this.clan_info.province_id = "";
				this.clan_info.city_id = "";
				this.clan_info.area_id = "";
				this.clan_info.town_id = "";
				this.clan_info.village_id = "";
			},
			//监听五级地址选中
			lpicker_select(e){
				this.clan_info.province_id = e.province_id;
				this.clan_info.city_id = e.city_id;
				this.clan_info.area_id = e.area_id;
				this.clan_info.town_id = e.town_id;
				this.clan_info.village_id = e.village_id;
			},
			//上传图片
			uploadimg(){
				uni.chooseImage({
					count:1,
					sourceType:['album','camera'],//album 从相册选图，camera 使用相机
					success: (e) => {
						uni.uploadFile({
						    url: uploadFile, 
						    filePath: e.tempFilePaths[0],
						    name: 'file',
							header:{
								'token':uni.getStorageSync("token"),
							},
						    formData: {
						       "Content-Type": "multipart/form-data",
						       water: 0
						    },
						    success: (res) => {
								let {data} = JSON.parse(res.data)
								console.log('多选',data);
								// data.url
								this.clan_info.photo = data.url;
								
						    },
							fail: (err) => {
								this.$api.msg("上传图片失败");
							}
						});
					}
				});
			},
			//清除照片
			deleteimg(){
				this.clan_info.photo = ''
			},
			async toFound(){
				if(!this.clan_info.firstname){
					this.$api.msg('请输入姓名！')
					return false;
				}				
				if(!this.clan_info.sex){
					this.$api.msg('请选择性别！')
					return false;
				}
				
				
				if(!this.clan_info.level){
					this.$api.msg('世代不得为空！')
					return false;
				}
				
				
				if(!this.clan_info.sex){
					this.$api.msg('请选择性别！')
					return false;
				}
				
				
				if(!this.clan_info.ext0 && this.clan_info.ext0!==0){
					this.$api.msg('请选择是否在世！')
					return false;
				}
				if(this.source!=1){
					//查询族员信息，防止重复添加成员
					// var res = await dimFamilyPeople({
					// 	skeyword:this.clan_info.firstname
					// })
					// if(res.code==1){
					// 	for(let i=0;i<res.data.length;i++){
					// 		if(res.data[i].firstname==this.clan_info.firstname&&res.data[i].sex==this.clan_info.sex&&res.data[i].ext0==this.clan_info.ext0&&res.data[i].ext5==this.clan_info.ext5){
					// 			this.query_result = res.data[i];
					// 		}
					// 		console.log(this.query_result,'this.query_result')
					// 	}
					// 	if(this.query_result.id){
					// 		this.is_model = true;
					// 		this.$showModal({
					// 			title:'提示',
					// 			concent:'族谱已存在该成员',
					// 			confirmVal:'修改信息',
					// 			cancelVal:'重新添加',
					// 		}).then(res=>{		
					// 			// 去修改信息
					// 			console.log(333)
					// 			uni.redirectTo({
					// 				url:`/pages/zupu/viewlifehistory/viewlifehistory?people_id=${this.query_result.id}&operate_name=${this.query_result.firstname}&family_id=${this.family_id}&identity_types=1&type=1`
					// 			})
					// 			return false;
					// 		}).catch(err=>{
					// 			this.query_result = {};
					// 			setTimeout(()=>{
					// 				this.$api.msg("请重新填写族员信息后再添加")
					// 				this.is_model = false;
					// 			},500)							
					// 			return false;
					// 		})
					// 	}
					// }	
				}
					
				let restwo = {};
				if(this.is_model){
					return false;
				}
				switch(this.source)
				{
				    case 1://添加配偶
						restwo = await addMateFamily({
							family_id:this.family_id,
							family_tree_id:this.family_tree_id,
							...this.clan_info,
						})
				        break;
				    case 2://添加父辈
						restwo = await addParentFamily({
							family_id:this.family_id,
							child_id:this.child_id,
							...this.clan_info,
						})
				        break;
					case 3://添加子辈
						restwo = await addChildFamily({
							family_id:this.family_id,
							parent_id:this.parent_id,
							...this.clan_info,
						})
					    break;
					case 4://初始化节点 创建初始成员
						restwo = await addCreateFamily({
							family_id:this.family_id,
							parent_id:this.parent_id,
							...this.clan_info,
						})
					    break;
				}
				this.$api.msg(restwo.msg)
				//添加成员
				if(restwo.code==1){
					if(restwo.data){
						this.people_number = restwo.data.number;
					}else{
						return false;
					}
					if(restwo.data.ext0==0 && this.people_number && this.userInfo.is_shaikh!=1){
						// && this.userInfo.is_shaikh!=1
						// people_number
						if(this.apple_isdebug && this.pu_type==2){//2是族谱才需要交钱 1家谱 2族谱  3宗谱
							uni.redirectTo({
								url:`/pages/zupu/join_pay/join_pay?number=${this.people_number}&family_tree_id=${this.family_tree_id}&family_id=${this.family_id}&source=${this.source}`
							})
							// uni.navigateTo({
							// 	url:`/pages/zupu/join_pay/join_pay?number=${this.people_number}&family_tree_id=${this.family_tree_id}&family_id=${this.family_id}&source=${this.source}`
							// })
						}else{
							//向下加载数据
							await this.ModifyItemData({family_id:this.family_id,family_tree_id:this.family_tree_id});
							uni.navigateBack();
						}
					}else{
						if(this.source==4){//初始化节点
							await this.familyTreeListfun({family_id:this.family_id,init_one:1,})
						}else if(this.source==2){//父辈
							await this.familyTreeListfun({family_id:this.family_id})
							//向上加载数据
							// await this.ModifyItemDataTop({family_id:this.family_id,family_tree_id:this.family_tree_id});
						}else{
							//向下加载数据
							await this.ModifyItemData({family_id:this.family_id,family_tree_id:this.family_tree_id});
						}
						
						setTimeout(()=>{
							uni.navigateBack();
						},1500)
					}
				}
			},
			//获取字辈
			genealogyListfun(){
				let type =''
				if(this.source==1){
					type = 0
				}
				if(this.source==2){
					type = 1
				}
				if(this.source==3){
					type = 2
				}
				console.log(type)
				genealogyList({
					family_id:this.family_id,
					people_id:this.people_id,
					type:type
				}).then(res=>{
					if(this.source!=4){
						this.clan_info.ext5 = res.data.val;	
						this.inferiority.push(res.data.val);
						console.log(this.inferiority)
					}else if(this.source==4){
						this.inferiority = res.data.genealogyArr;
					}
					
				})
			},
			//选择字辈
			bindPickerChange(e) {
			    this.clan_info.ext5 = this.inferiority[e.detail.value];
				console.log(this.clan_info.ext5)
			},
			//修改学校信息
			toschool(index){
				uni.navigateTo({
					url:`/pages/mine/myschool/myschool?index=${index}`
				})
			},
			//更改工作经历
			towork(index){
				uni.navigateTo({
					url:`/pages/mine/addcompany/addcompany?index=${index}`
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "./createnode.scss";
</style>
